using System;
using System.Collections.Generic;
using SharpPcap.Packets;
using SharpPcap;

namespace sniffer
{

    public class Sniffer
    {
   
        public static void Main(string[] args)
        {
            
            Console.WriteLine("Welcome to the sniffer developed in C# ");
            Console.WriteLine();
			//args.Length
			string capturedevice;
			if(args.Length==0)
			{
					capturedevice = "eth0";
				Console.WriteLine("No device entered, chose eth0 as capture device");
			}
			else
			{
				capturedevice = args[0];
				Console.WriteLine("Device you want to capture with is: {0}", capturedevice);
			}
			
				
				

            // Retrieve the device list 
            List<PcapDevice> devices = SharpPcap.Pcap.GetAllDevices();

            /*If no device exists, print error */
            if(devices.Count<1)
            {
                Console.WriteLine("No device found on this machine");
                return;
            }
            
      

            int i=0,j=-1;
			
            /* Scan the list printing every entry to check if the chosen device is available */
            foreach(PcapDevice dev in devices)
            {
      
				if (dev.Name == capturedevice)
				{				
					j=i;						
					break;
				}
				i++;
            }
			/* if device isn't available display a err message*/
			if(j==-1)
			{
				Console.WriteLine("Wrong device entered try ifconfig to retrieve a list with internet devices on your system");		
				Environment.Exit(1);
			}
 			PcapDevice device = devices[j];
			Console.WriteLine("Chosen valid device is: {0}", device.Name);
   

            device.OnPacketArrival += 
                new SharpPcap.Pcap.PacketArrivalEvent( device_PcapOnPacketArrival );

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means a read wait of 1000ms
            device.Open(true, 1000);

            //tcpdump filter to capture only TCP/IP packets         
            string filter = "ip";
            //Associate the filter with this capture
            device.SetFilter( filter );

            Console.WriteLine();
            Console.WriteLine
                ("-- The following tcpdump filter will be applied: \"{0}\"", 
                filter);
            Console.WriteLine
                ("-- Listenning on {0}, hit 'Ctrl-C' to exit...",
                device.Description);

            //Start capture 'INFINTE' number of packets
            device.Capture( SharpPcap.Pcap.INFINITE );

            //Close the pcap device
            //(Note: this line will never be called since
            // we're capturing infinite number of packets
            device.Close();
        }

    
        private static void device_PcapOnPacketArrival(object sender, PcapCaptureEventArgs e)
        {           
            if(e.Packet is TCPPacket)
            {               
               
                uint len = e.Packet.PcapHeader.PacketLength;

                TCPPacket tcp = (TCPPacket)e.Packet;
				System.Net.NetworkInformation.PhysicalAddress srcMac = tcp.SourceHwAddress;
				System.Net.NetworkInformation.PhysicalAddress dstMac = tcp.DestinationHwAddress;
                System.Net.IPAddress srcIp = tcp.SourceAddress;
                System.Net.IPAddress dstIp = tcp.DestinationAddress;
                int srcPort = tcp.SourcePort;
                int dstPort = tcp.DestinationPort;
				
				
				Console.WriteLine("*******************************************************");
				Console.WriteLine("Source Mac Address: {0}" , srcMac);
				Console.WriteLine("Destination Mac Address : {0}",dstMac);				
				Console.WriteLine("Source IP address: {0}", srcIp);
				Console.WriteLine("Destination IP address {0}", dstIp);
				Console.WriteLine("Sourceport: {0} to Destinationport: {1}", srcPort,dstPort); 		
				Console.WriteLine("Packet Length: {0}", len);
				Console.WriteLine("Type: TCP");
				Console.WriteLine("*******************************************************");
				
            }

			
			if (e.Packet is UDPPacket)
			{      
               
                uint len = e.Packet.PcapHeader.PacketLength;

                UDPPacket udp = (UDPPacket)e.Packet;
				System.Net.NetworkInformation.PhysicalAddress srcMac = udp.SourceHwAddress;
				System.Net.NetworkInformation.PhysicalAddress dstMac = udp.DestinationHwAddress;
                System.Net.IPAddress srcIp = udp.SourceAddress;
                System.Net.IPAddress dstIp = udp.DestinationAddress;
                int srcPort = udp.SourcePort;
                int dstPort = udp.DestinationPort;
				
				Console.WriteLine("*******************************************************");
				Console.WriteLine("Source Mac Address: {0}" , srcMac);
				Console.WriteLine("Destination Mac Address : {0}",dstMac);				
				Console.WriteLine("Source IP address: {0}", srcIp);
				Console.WriteLine("Destination IP address {0}", dstIp);
				Console.WriteLine("Sourceport: {0} to Destinationport: {1}", srcPort,dstPort); 		
				Console.WriteLine("Packet Length: {0}", len);
				Console.WriteLine("Type: UDP");
				Console.WriteLine("*******************************************************");
				
            
			}
			
        }
    }
}